package cu.tedu._05mvcboot03.excepetion;

import cu.tedu._05mvcboot03.commom.response.JsonResult;
import cu.tedu._05mvcboot03.commom.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult MethodArgumentNotValidException(MethodArgumentNotValidException exception){
        String message = exception.getFieldError().getDefaultMessage();
        log.error("MethodArgumentNotValidException:"+message);
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }
    @ExceptionHandler
    public JsonResult ConstraintViolationException(ConstraintViolationException exception){
        String message = exception.getMessage().split(":")[1].trim();
        log.error("ConstraintViolationException:"+message);
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }
}
